Skip to contentMethod: setValue(ELContext, Object)
1: /*
2: * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package com.sun.el;
18:
19: import static com.sun.el.util.ReflectionUtil.forName;
20:
21: import java.io.Externalizable;
22: import java.io.IOException;
23: import java.io.ObjectInput;
24: import java.io.ObjectOutput;
25:
26: import jakarta.el.ELContext;
27: import jakarta.el.ELException;
28: import jakarta.el.ELResolver;
29: import jakarta.el.Expression;
30: import jakarta.el.ExpressionFactory;
31: import jakarta.el.FunctionMapper;
32: import jakarta.el.PropertyNotFoundException;
33: import jakarta.el.PropertyNotWritableException;
34: import jakarta.el.ValueExpression;
35: import jakarta.el.ValueReference;
36: import jakarta.el.VariableMapper;
37:
38: import com.sun.el.lang.EvaluationContext;
39: import com.sun.el.lang.ExpressionBuilder;
40: import com.sun.el.parser.AstLiteralExpression;
41: import com.sun.el.parser.Node;
42:
43: /**
44: * An <code>Expression</code> that can get or set a value.
45: *
46: * <p>
47: * In previous incarnations of this API, expressions could only be read. <code>ValueExpression</code> objects can now be
48: * used both to retrieve a value and to set a value. Expressions that can have a value set on them are referred to as
49: * l-value expressions. Those that cannot are referred to as r-value expressions. Not all r-value expressions can be
50: * used as l-value expressions (e.g. <code>"${1+1}"</code> or <code>"${firstName} ${lastName}"</code>). See the EL
51: * Specification for details. Expressions that cannot be used as l-values must always return <code>true</code> from
52: * <code>isReadOnly()</code>.
53: * </p>
54: *
55: * <p>
56: * The {@link ExpressionFactory#createValueExpression} method can be used to parse an expression string and return a
57: * concrete instance of <code>ValueExpression</code> that encapsulates the parsed expression. The {@link FunctionMapper}
58: * is used at parse time, not evaluation time, so one is not needed to evaluate an expression using this class. However,
59: * the {@link ELContext} is needed at evaluation time.
60: * </p>
61: *
62: * <p>
63: * The {@link #getValue}, {@link #setValue}, {@link #isReadOnly} and {@link #getType} methods will evaluate the
64: * expression each time they are called. The {@link ELResolver} in the <code>ELContext</code> is used to resolve the
65: * top-level variables and to determine the behavior of the <code>.</code> and <code>[]</code> operators. For any of the
66: * four methods, the {@link ELResolver#getValue} method is used to resolve all properties up to but excluding the last
67: * one. This provides the <code>base</code> object. At the last resolution, the <code>ValueExpression</code> will call
68: * the corresponding {@link ELResolver#getValue}, {@link ELResolver#setValue}, {@link ELResolver#isReadOnly} or
69: * {@link ELResolver#getType} method, depending on which was called on the <code>ValueExpression</code>.
70: * </p>
71: *
72: * <p>
73: * See the notes about comparison, serialization and immutability in the {@link Expression} javadocs.
74: *
75: * @see ELResolver
76: * @see Expression
77: * @see ExpressionFactory
78: * @see ValueExpression
79: *
80: * @author Jacob Hookom [jacob@hookom.net]
81: * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: dochez $
82: */
83: public final class ValueExpressionImpl extends ValueExpression implements Externalizable {
84:
85: private Class<?> expectedType;
86: private String expr;
87: private FunctionMapper fnMapper;
88: private VariableMapper varMapper;
89: private transient Node node;
90:
91: public ValueExpressionImpl() {
92:
93: }
94:
95: public ValueExpressionImpl(String expr, Node node, FunctionMapper fnMapper, VariableMapper varMapper, Class<?> expectedType) {
96: this.expr = expr;
97: this.node = node;
98: this.fnMapper = fnMapper;
99: this.varMapper = varMapper;
100: this.expectedType = expectedType;
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see java.lang.Object#equals(java.lang.Object)
107: */
108: @Override
109: public boolean equals(Object obj) {
110: if (obj instanceof ValueExpressionImpl) {
111: ValueExpressionImpl valueExpressionImpl = (ValueExpressionImpl) obj;
112: return getNode().equals(valueExpressionImpl.getNode());
113: }
114:
115: return false;
116: }
117:
118: /*
119: * (non-Javadoc)
120: *
121: * @see ValueExpression#getExpectedType()
122: */
123: @Override
124: public Class<?> getExpectedType() {
125: return expectedType;
126: }
127:
128: /**
129: * Returns the type the result of the expression will be coerced to after evaluation.
130: *
131: * @return the <code>expectedType</code> passed to the <code>ExpressionFactory.createValueExpression</code> method that
132: * created this <code>ValueExpression</code>.
133: *
134: * @see Expression#getExpressionString()
135: */
136: @Override
137: public String getExpressionString() {
138: return expr;
139: }
140:
141: /**
142: * @return The Node for the expression
143: * @throws ELException
144: */
145: private Node getNode() throws ELException {
146: if (node == null) {
147: node = ExpressionBuilder.createNode(expr);
148: }
149:
150: return this.node;
151: }
152:
153: /*
154: * (non-Javadoc)
155: *
156: * @see ValueExpression#getType(ELContext)
157: */
158: @Override
159: public Class<?> getType(ELContext context) throws PropertyNotFoundException, ELException {
160: return getNode().getType(new EvaluationContext(context, fnMapper, varMapper));
161: }
162:
163: /*
164: * (non-Javadoc)
165: *
166: * @see ValueExpression#getValueReference(ELContext)
167: */
168: @Override
169: public ValueReference getValueReference(ELContext context) throws PropertyNotFoundException, ELException {
170: return getNode().getValueReference(new EvaluationContext(context, fnMapper, varMapper));
171: }
172:
173: /*
174: * (non-Javadoc)
175: *
176: * @see ValueExpression#getValue(ELContext)
177: */
178: @Override
179: public Object getValue(ELContext context) throws PropertyNotFoundException, ELException {
180: EvaluationContext ctx = new EvaluationContext(context, fnMapper, varMapper);
181: ctx.notifyBeforeEvaluation(expr);
182:
183: Object value = getNode().getValue(ctx);
184:
185: if (expectedType != null) {
186: try {
187: value = ctx.convertToType(value, expectedType);
188: } catch (IllegalArgumentException ex) {
189: throw new ELException(ex);
190: }
191: }
192: ctx.notifyAfterEvaluation(expr);
193: return value;
194: }
195:
196: /*
197: * (non-Javadoc)
198: *
199: * @see java.lang.Object#hashCode()
200: */
201: @Override
202: public int hashCode() {
203: return getNode().hashCode();
204: }
205:
206: /*
207: * (non-Javadoc)
208: *
209: * @see ValueExpression#isLiteralText()
210: */
211: @Override
212: public boolean isLiteralText() {
213: try {
214: return getNode() instanceof AstLiteralExpression;
215: } catch (ELException ele) {
216: return false;
217: }
218: }
219:
220: /*
221: * (non-Javadoc)
222: *
223: * @see ValueExpression#isReadOnly(ELContext)
224: */
225: @Override
226: public boolean isReadOnly(ELContext context) throws PropertyNotFoundException, ELException {
227: return getNode().isReadOnly(new EvaluationContext(context, fnMapper, varMapper));
228: }
229:
230: @Override
231: public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
232: expr = in.readUTF();
233: String type = in.readUTF();
234: if (!"".equals(type)) {
235: expectedType = forName(type);
236: }
237: fnMapper = (FunctionMapper) in.readObject();
238: varMapper = (VariableMapper) in.readObject();
239: }
240:
241: /*
242: * (non-Javadoc)
243: *
244: * @see ValueExpression#setValue(ELContext, java.lang.Object)
245: */
246: @Override
247: public void setValue(ELContext context, Object value) throws PropertyNotFoundException, PropertyNotWritableException, ELException {
248: getNode().setValue(new EvaluationContext(context, fnMapper, varMapper), value);
249: }
250:
251: @Override
252: public void writeExternal(ObjectOutput out) throws IOException {
253: out.writeUTF(expr);
254: out.writeUTF(expectedType != null ? expectedType.getName() : "");
255: out.writeObject(fnMapper);
256: out.writeObject(varMapper);
257: }
258:
259: @Override
260: public String toString() {
261: return "ValueExpression[" + expr + "]";
262: }
263: }